package com.wz.utils.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamUtils {
    public static void main(String[] args) {
        Region r1 = new Region("ABC", "10001", 0);
        Region r2 = new Region("ABC", "10002", 0);
        Region r3 = new Region("ABC", "10003", 1);

        Region r4 = new Region("DEF", "10001", 1);
        Region r5 = new Region("DEF", "10002", 0);
        Region r6 = new Region("DEF", "10003", 0);

        List<Region> regions = new ArrayList<>();
        regions.add(r1);
        regions.add(r2);
        regions.add(r3);
        regions.add(r4);
        regions.add(r5);
        regions.add(r6);

        Map<String, List<Region>> regionMap = regions.stream().collect(Collectors.groupingBy(item -> item.getRegionCode() + " " + item.getRole()));
        Map<String, Map<Integer, List<Region>>> map = regions.stream().collect(Collectors.groupingBy(Region::getRegionCode,
                Collectors.groupingBy(Region::getRole)));
        System.out.println(regionMap);
        System.out.println(map);
    }


    static class Region {
        private String regionCode;
        private String userId;
        private Integer role;

        public Region(String regionCode, String userId, Integer role) {
            this.regionCode = regionCode;
            this.userId = userId;
            this.role = role;
        }

        @Override
        public String toString() {
            return "Region{" +
                    "regionCode='" + regionCode + '\'' +
                    ", userId='" + userId + '\'' +
                    ", role=" + role +
                    '}';
        }

        public String getRegionCode() {
            return regionCode;
        }

        public void setRegionCode(String regionCode) {
            this.regionCode = regionCode;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public Integer getRole() {
            return role;
        }

        public void setRole(Integer role) {
            this.role = role;
        }
    }
}



